﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UFIDA.U9.Cust.XH.PlugBE.Tool;
using UFIDA.U9.Cust.XH.PlugBE.Model;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using Newtonsoft.Json;

namespace UFIDA.U9.Cust.XH.PlugBE
{
    /// <summary>
    /// 供应商
    /// 审核
    /// </summary>
    class SupplierCheckExtend : IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            #region 从事件参数中取得当前业务实体													 

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
             if (key == null)
                return;
            UFIDA.U9.CBO.SCM.Supplier.Supplier entity = key.GetEntity() as UFIDA.U9.CBO.SCM.Supplier.Supplier;
            if (entity == null)
                return;

            #endregion
            if (PubWebClass.IsSyncToOA() && entity.State == CBO.SCM.Supplier.SupplierStateEnum.Verified && entity.OriginalData.State != CBO.SCM.Supplier.SupplierStateEnum.Verified)
            {
                SupdataModel model = new SupdataModel();
                Dictionary<string, string> opeartion = new Dictionary<string, string>();
                opeartion.Add("operationDate", DateTime.Now.ToString("yyyy-MM-dd"));
                opeartion.Add("operationTime", DateTime.Now.ToString("HH:mm:ss"));
                opeartion.Add("operator", "1");
                model.operationinfo = opeartion;
                OASupModel o = new OASupModel(entity);
                model.mainTable = o;
                List<SupdataModel> data = new List<SupdataModel>();
                data.Add(model);
                string res = PubWebClass.RecordHttp("同步供应商", OAUrlHelper.getSuppUrl, data);
                if (res.Contains("异常信息"))
                {
                    throw new Exception(res);
                }
                else
                {
                    JObject jobj = JObject.Parse(res);

                    JObject d = JObject.Parse(jobj["datajson"].ToString());
                    if (!string.IsNullOrEmpty(d["faildata"].ToString()))
                    {
                        JArray ary = JArray.Parse(d["faildata"].ToString());
                        string msg = "";
                        foreach (dynamic item in ary)
                        {
                            SupdataModel dd = JsonConvert.DeserializeObject<SupdataModel>(item["originaldata"].ToString());                            
                            msg += dd.mainTable.gysbm + ":" + item["dataSourceError"]?.ToString() + item["mainTable"]?.ToString() + "；";

                        }
                        if (!string.IsNullOrEmpty(msg))
                        {
                            throw new Exception("供应商同步OA失败" + msg);
                        }
                    }

                }
            }

        }
    }
}
